package com.stu.stusystem.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cxt
 * @time: 2021/3/29
 * 树形数据工具
 * 注意，传入的对象属性中父级id名为pid，如果不是就将下面 “pid” 名改成自己的
 */
@Data
@Slf4j
public class TreeModelUtil {

    public static List<Object> buildTreeList(List<?> tm, String pid) {
        if (tm.size() == 0) {
            return null;
        }
        List<Object> list = new ArrayList<>(); // 最后返回的数据
        try {
            for (Object obj : tm) {
                Map<String, Object> mapArr = new LinkedHashMap<>();
                // 取值
                Class<?> clz = obj.getClass();
                Field[] temp = clz.getDeclaredFields();
                boolean msg = false;
                for (Field f : temp) {
                    f.setAccessible(true); // 可以访问私有变量
                    String value = String.valueOf(f.get(obj));
                    if (f.getName().equals(pid) && !value.equals("")) {
                        if (!value.equals("null")) {
                            msg = true;
                            break;
                        }
                    }
                    mapArr.put(f.getName(), value);
                }
                if (msg) {
                    continue;
                }
                // 查找child
                String id = (String) mapArr.get("id");
                List<?> treeList = getTreeList(id, tm, pid);
                if (treeList.size() != 0) {
                    mapArr.put("child", treeList);
                }
                list.add(mapArr);
            }
        } catch (IllegalAccessException e) {
            log.error("树形工具类出错");
        }
        return list;
    }

    public static List<?> getTreeList(String id, List<?> treeModels, String pid) {
        List<Object> lists = new ArrayList<>();
        try {
            for (Object obj : treeModels) {
                Map<String, Object> mapArr = new LinkedHashMap<>();
                // 取值
                Class<?> clz = obj.getClass();
                Field[] temp = clz.getDeclaredFields();
                boolean msg = false;
                for (Field f : temp) {
                    f.setAccessible(true); // 可以访问私有变量
                    String value = String.valueOf(f.get(obj));
                    if (f.getName().equals(pid) && !value.equals(id)) {
                        msg = true;
                        break;
                    }
                    mapArr.put(f.getName(), value);
                }
                if (msg) {
                    continue;
                }
                // 查找child
                String childId = (String) mapArr.get("id");
                List<?> treeList = getTreeList(childId, treeModels, pid);
                if (treeList.size() != 0) {
                    mapArr.put("child", treeList);
                }
                lists.add(mapArr);
            }
        } catch (IllegalAccessException e) {
            log.error("树形工具类出错");
        }
        return lists;
    }
}
